//
//  TLTweetDetailView.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/23.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLTweetDetailView.h"
#import "TLTweetDetailViewFrame.h"
#import "TLTweetPhotosView.h"
#import "TTTAttributedLabel.h"
#import "TLTweet.h"
#import "TLOwner.h"
#import "TLHtmlMedia.h"
#import "TLHtmlMediaItem.h"

@interface TLTweetDetailView()

@property (nonatomic, weak) UIImageView *headerIconView;

@property (nonatomic, weak) UILabel *nameLabel;

@property (nonatomic, weak) UILabel *timeLabel;

@property (nonatomic, weak) TTTAttributedLabel *contentLabel;

@property (nonatomic, weak) UILabel *locationLabel;

@property (nonatomic, weak) UIImageView *fromIcon;

@property (nonatomic, weak) UILabel *fromLabel;

@property (nonatomic, weak) TLTweetPhotosView *tweetPhotosView;

@end

@implementation TLTweetDetailView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        
        UIImageView *headerIconView = [[UIImageView alloc] init];
        [self addSubview:headerIconView];
        self.headerIconView = headerIconView;
        
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.textColor = TLColor(153, 161, 170, 1.0);
        timeLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        TTTAttributedLabel *contentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(20, 20, TLScreenW - 40, 20)];
        contentLabel.textColor = [UIColor colorWithHexString:@"0x323A45"];
        contentLabel.numberOfLines = 0;
        contentLabel.font = [UIFont systemFontOfSize:16];
        contentLabel.linkAttributes = kLinkAttributes;
        contentLabel.activeLinkAttributes = kLinkAttributesActive;
        [self addSubview:contentLabel];
        self.contentLabel = contentLabel;
        
        UILabel *locationLabel = [[UILabel alloc] init];
        locationLabel.font = [UIFont systemFontOfSize:12];
        locationLabel.textColor = TLColor(0, 179, 122, 1.0);
        [self addSubview:locationLabel];
        self.locationLabel = locationLabel;
        
        UIImageView *fromIcon = [[UIImageView alloc] initWithImage:[UIImage originImageWithName:@"little_phone_icon"]];
        [self addSubview:fromIcon];
        self.fromIcon = fromIcon;
        
        UILabel *fromLabel = [[UILabel alloc] init];
        fromLabel.font = [UIFont systemFontOfSize:12];
        fromLabel.textColor = [UIColor colorWithHexString:@"0x76808E"];
        [self addSubview:fromLabel];
        self.fromLabel = fromLabel;
        
        TLTweetPhotosView *tweetPhotosView = [[TLTweetPhotosView alloc] init];
        [self addSubview:tweetPhotosView];
        self.tweetPhotosView = tweetPhotosView;
    }
    return self;
}

- (void)setDetailFrame:(TLTweetDetailViewFrame *)detailFrame
{
    _detailFrame = detailFrame;
    
    self.frame = detailFrame.frame;
    
    // 取出冒泡数据
    TLTweet *tweet = detailFrame.tweet;
    // 取出用户数据
    TLOwner *owner = tweet.owner;
    
    // 1. 头像
    [self.headerIconView setCircleHeader:owner.avatar];
    self.headerIconView.frame = detailFrame.iconFrame;
    
    // 2. 昵称
    self.nameLabel.text = owner.name;
    self.nameLabel.frame = detailFrame.nameFrame;
    
    // 3. 发布时间
    NSString *time = tweet.created_at;
    self.timeLabel.text = time;
    CGFloat timeX = CGRectGetMinX(self.nameLabel.frame);
    CGFloat timeY = CGRectGetMaxY(self.nameLabel.frame) + 5;
    CGFloat timeH = 20.0;
    CGSize timeSize = [time boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine| NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
    self.timeLabel.frame = CGRectMake(timeX, timeY, timeSize.width, timeH);
    
    // 4. 内容
    self.contentLabel.text = tweet.content;
    self.contentLabel.frame = detailFrame.contentFrame;
    for (TLHtmlMediaItem *item in tweet.htmlMedia.mediaItems) {
        if (item.displayStr.length > 0 && item.href.length > 0) {
            [self.contentLabel addLinkToTransitInformation:[NSDictionary dictionaryWithObject:item forKey:@"value"] withRange:item.range];
        }
    }
    
    // 5. 配图
    CGFloat locationLabelY = 0;
    if(tweet.htmlMedia.imageItems.count <= 0){
        self.tweetPhotosView.hidden = YES;
        locationLabelY = CGRectGetMaxY(self.contentLabel.frame) + 10;
    }else{
        
        self.tweetPhotosView.frame = detailFrame.photosFrame;
        self.tweetPhotosView.htmlMedias = tweet.htmlMedia.imageItems;
        if(tweet.htmlMedia.imageItems.count == 1){
            
            self.tweetPhotosView.size = [self.tweetPhotosView sizeWithHtmlMediaImageCount:tweet.htmlMedia.imageItems];
        }
        self.tweetPhotosView.hidden = NO;
        locationLabelY = CGRectGetMaxY(self.tweetPhotosView.frame) + 10;
    }
    
    // 6. 地址
    CGFloat fromIconY = 0;
    self.fromIcon.hidden = !tweet.device.length;
    self.fromLabel.hidden = !tweet.device.length;
    self.fromLabel.text = [NSString stringWithFormat:@"来自%@", tweet.device];
    [self.fromLabel sizeToFit];
    if(tweet.location.length > 0){
        self.locationLabel.hidden = NO;
        self.locationLabel.text = tweet.location;
        [self.locationLabel sizeToFit];
        self.locationLabel.frame = detailFrame.locationLabelFrame;
        fromIconY = CGRectGetMaxY(self.locationLabel.frame) + 10;
    }else{
        self.locationLabel.hidden = YES;
        if(tweet.htmlMedia.imageItems.count > 0){
            
            fromIconY = CGRectGetMaxY(self.tweetPhotosView.frame) + 10;
        }else{
            
            fromIconY = CGRectGetMaxY(self.contentLabel.frame) + 10;
        }
    }
    self.fromIcon.frame = CGRectMake(self.contentLabel.x, fromIconY, self.fromIcon.image.size.width, self.fromIcon.image.size.height);
    self.fromLabel.frame = CGRectMake(CGRectGetMaxX(self.fromIcon.frame) + 10, self.fromIcon.y, self.fromLabel.width, 12);
}

@end
